package cn.exrick.xboot.modules.AppUtils.controller;

import cn.exrick.xboot.base.XbootBaseController;
import cn.exrick.xboot.common.formDto.AppBacklogAngle;
import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.common.vo.SearchVo;
import cn.exrick.xboot.modules.AppUtils.entity.AppUtils;
import cn.exrick.xboot.modules.AppUtils.entity.ContentVo;
import cn.exrick.xboot.modules.AppUtils.service.AppUtilsService;
import cn.exrick.xboot.modules.backlogOrFullList.service.IbacklogOrFullListService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;

/**
 * @author jiuqi
 */
@Slf4j
@RestController
@Api(description = "AppUtils管理接口")
@RequestMapping("/xboot/app")
@Transactional
public class AppUtilsController extends XbootBaseController<AppUtils, String> {

    @Autowired
    private AppUtilsService appUtilsService;

    @Autowired
    private IbacklogOrFullListService ibacklogOrFullListService;

    @Override
    public AppUtilsService getService() {
        return appUtilsService;
    }

    @RequestMapping(value = "/getByCondition", method = RequestMethod.GET)
    @ApiOperation(value = "多条件分页获取")
    public Result<Page<AppUtils>> getByCondition(@ModelAttribute AppUtils appUtils,
                                                            @ModelAttribute SearchVo searchVo,
                                                            @ModelAttribute PageVo pageVo){

        Page<AppUtils> page = appUtilsService.findByCondition(appUtils, searchVo, PageUtil.initPage(pageVo));
        return new ResultUtil<Page<AppUtils>>().setData(page);
    }

    @RequestMapping(value = "/getAppDownloadlink", method = RequestMethod.GET)
    @ApiOperation(value = "获取APP下载地址")
    public Result<List<AppUtils>> getDownloadlink() {
        List<AppUtils> appUtilsList=new ArrayList<>();
        List<AppUtils> all = appUtilsService.getAll();
        if(all.size()==2){
            for (AppUtils appUtils : all) {
                appUtilsList.add(appUtils);
            }
        }else{
            AppUtils  appIos=new AppUtils();
            AppUtils  appAndroid=new AppUtils();
            for (AppUtils appUtils : all) {
                if("ios".equals(appUtils.getAppCode())){
                    appIos=appUtils;
                }
                if("Android".equals(appUtils.getAppCode())){
                    appAndroid=appUtils;
                }
            }
            appUtilsList.add(appIos);
            appUtilsList.add(appAndroid);
        }
        return new ResultUtil<List<AppUtils>>().setData(appUtilsList);
    }

    @RequestMapping(value = "/getInfoContent", method = RequestMethod.GET)
    @ApiOperation(value = "统一app端的新闻公告信息的展示返回接口")
    public Result<ContentVo> getInfoContent(int variety,String  id) {
        ContentVo infoContent = appUtilsService.getInfoContent(variety,id);
        log.info(infoContent.toString());
        return new ResultUtil<ContentVo>().setData(infoContent);
    }

    /**
     *1:查询出当前登录用户待办的总数
     * 2：将待办返回给app端
     *
     */
    @RequestMapping(value = "/getBacklogAngle", method = RequestMethod.GET)
    @ApiImplicitParam(name = "username", value = "用户名（注意这个用户名不是手机号，是登录成功返回登录信息中的账号）")
    @ApiOperation(value = "统一app端的新闻公告信息的展示返回接口")
    public Result<AppBacklogAngle> getBacklogAngle(String username) {
        // 1:查询出当前待办的总数
        log.info("查询当前用户的待办数量");
        Integer num = ibacklogOrFullListService.selectBacklogAngleNum(username);
        log.info("用户"+username+"的待办数量是："+num);
        AppBacklogAngle appBacklogAngle=new AppBacklogAngle();
        appBacklogAngle.setUsername(username);
        appBacklogAngle.setNum(num);
        return new ResultUtil<AppBacklogAngle>().setData(appBacklogAngle);
    }

   /* @RequestMapping(value = "/test", method = RequestMethod.GET)
    @ApiOperation(value = "获取APP下载地址")
    public Result<List<AppUtils>> test() {
        List<AppUtils> appUtilsList=new ArrayList<>();
        AppUtils  app1=new AppUtils();
        app1.setAppCode("ios");
        //app1.setDownloadLink("itms-services://?action=download-manifest&url=https://gitee.com/lichengCode/plist/raw/master/manifest.plist");
        app1.setDownloadLink("https://fir.im/rjmb");
        AppUtils  app2=new AppUtils();
        app2.setAppCode("Android");
        //app2.setDownloadLink("http://47.92.27.141:8899/xboot/downloadResources/downloadFile?fileName=app-release.apk");
        app2.setDownloadLink("https://fir.im/rjmb");
        appUtilsList.add(app1);
        appUtilsList.add(app2);

        return new ResultUtil<List<AppUtils>>().setData(appUtilsList);
    }*/

}
